package one.stand.controller.category;

import com.github.pagehelper.PageHelper;
import com.jmp.api.dto.*;
import com.jmp.api.util.LabelUtil;
import com.jmp.feign.AppIndexGoodsFeign;
import com.jmp.feign.BannerFeign;
import com.jmp.service.api.admin.AppIndexGoodsApi;
import com.jmp.service.api.admin.BannerApi;
import com.jmp.service.api.admin.dto.AppIndexGoodsRequest;
import com.jmp.service.api.admin.dto.AppIndexGoodsResponse;
import com.jmp.service.api.admin.dto.BannerListDto;
import com.jmp.service.api.admin.dto.BannerListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.dto.Company2Dto;
import one.stand.dto.ConvertUtils;
import one.stand.dto.GoodsTypeDto;
import one.stand.dto.PageResponse2;
import one.stand.enums.SeeRecordEnums;
import one.stand.mapper.GoodsApiCrmAppMapper;
import one.stand.model.Goods;
import one.stand.model.GoodsType;
import one.stand.model.ResultModel;
import one.stand.service.*;
import one.stand.util.AssertUtil;
import one.stand.util.OneListUtil;
import one.stand.util.OneStringUtils;
import one.stand.util.SqlUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @author cjr
 */
@Api(description = "分类")
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private AppIndexGoodsFeign appIndexGoodsFeign;
    @Autowired
    private BannerFeign bannerFeign;

    @Autowired
    private GoodsTypeService goodsTypeService;
    @Autowired
    private GoodsBaseService goodsBaseService;
    @Autowired
    private RecordPublicService recordPublicService;
    @Autowired
    private CompanyBaseService companyBaseService;
    @Autowired
    private GoodsPhotoService goodsPhotoService;


    @Autowired
    private GoodsApiCrmAppMapper goodsApiCrmAppMapper;

    @ApiOperation("1一级分类")
    @GetMapping
    public ResultModel<CategoryResponse> index() {

        List<GoodsType> goodsTypes = goodsTypeService.getParent(GoodsTypeService.PLATFORM_COMPANY_ID);
        List<CategoryResponse.Type> goodsTypeList = ConvertUtils.list(goodsTypes, goodsType -> {
            return CategoryResponse.Type.builder()
                    .typeId(goodsType.getGoodstypeId())
                    .typeName(goodsType.getName())
                    .build();
        });

        BannerListDto bannerListDto = BannerListDto.builder()
                .companyId(BannerApi.COMPANY_ID_PLATFORM)
                .bannerType(BannerApi.BANNER_TYPE_JSX_TYPE)
                .refId(null)
                .build();
        bannerListDto.setAll();
        ResultModel<BannerListVo> bannerListVoResultModel = bannerFeign.list(bannerListDto);
        List<CategoryResponse.Banner> bannerList = ConvertUtils.list(bannerListVoResultModel.getData().getBannerPage().getList(), companyBanner -> CategoryResponse.Banner.builder()
                .source(companyBanner.getSource())
                .event(companyBanner.getEvent())
                .eventId(String.valueOf(companyBanner.getId()))
                .build());

        CategoryResponse response = CategoryResponse.builder()
                .typeList(goodsTypeList)
                .bannerList(bannerList)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation("2二级分类")
    @GetMapping("/child")
    public ResultModel<CategoryChildResponse> child(@RequestAttribute CategoryChildRequest request) {

        List<GoodsType> goodsTypes = goodsTypeService.getChild(request.getTypeId(), null);
        List<CategoryChildResponse.Type> goodsTypeList = ConvertUtils.list(goodsTypes, goodsType -> {
            return CategoryChildResponse.Type.builder()
                    .typeId(goodsType.getGoodstypeId())
                    .typeName(goodsType.getName())
                    .photo(goodsType.getPhoto())
                    .build();
        });

        CategoryChildResponse response = CategoryChildResponse.builder()
                .typeList(goodsTypeList)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation("3产品列表")
    @GetMapping("/goods")
    public ResultModel<CategoryGoodsResponse> goods(@RequestAttribute CategoryGoodsRequest request) {

        String nameLike = SqlUtil.like(request.getName());
        PageResponse2<Integer> goodsIds;
        if (Objects.nonNull(request.getBannerId())) {//搜索配置商品列表
            AppIndexGoodsRequest goodsRequest = AppIndexGoodsRequest.builder()
                    .type(AppIndexGoodsApi.TYPE_CATEGORY)
                    .refId(request.getBannerId())
                    .nameLike(nameLike)
                    .build();
            goodsRequest.setPageNum(request.getPageNum());
            goodsRequest.setPageSize(request.getPageSize());
            ResultModel<AppIndexGoodsResponse> responseResultModel = appIndexGoodsFeign.list(goodsRequest);
            AssertUtil.isTrue(responseResultModel);
            goodsIds = ConvertUtils.page2(responseResultModel.getData().getGoodsList(), AppIndexGoodsResponse.Goods::getGoodsId);
        } else {//搜索商品库
            PageHelper.startPage(request.getPageNum(), request.getPageSize());
            List<Integer> goodsIdList = goodsApiCrmAppMapper.selectSearch(nameLike, null, request.getTypeId());
            goodsIds = ConvertUtils.page2(goodsIdList, id -> id);
        }

        PageResponse2<CategoryGoodsResponse.Goods> goodsList = ConvertUtils.page2(goodsIds, goodsId -> {
            List<GoodsTypeDto> platformTypes = goodsBaseService.getTypes(goodsId);
            if (OneListUtil.isEmpty(platformTypes)) return null;

            Goods goods = goodsBaseService.get(goodsId);
            Long popularityNum = recordPublicService.getPopularity(SeeRecordEnums.PRODUCT.getCode(), goods.getGoodsId());
            boolean hasVr = StringUtils.isNoneBlank(goods.getQjVrUrl());
            boolean hasAr = goodsBaseService.hasAr(goodsId);
            boolean hasVideo = StringUtils.isNoneBlank(goods.getVideoUrl());
            boolean hasZkgx = companyBaseService.hasZkgx(goods.getCompanyId());
            Company2Dto company = companyBaseService.get2(goods.getCompanyId());

            return CategoryGoodsResponse.Goods.builder()
                    .goodsId(goodsId)
                    .goodsName(goods.getGoodsName())
                    .photo(OneStringUtils.photoResize(goodsPhotoService.getPath(goods.getGoodsId())))
                    .labels(LabelUtil.getLabels(hasVr, hasAr, hasVideo, hasZkgx))
                    .companyName(Objects.nonNull(company) ? company.getCompanyName() : null)
                    .popularityNum(popularityNum)
                    .build();
        });

        CategoryGoodsResponse response = CategoryGoodsResponse.builder()
                .goodsPage(goodsList)
                .build();
        return ResultModel.success(response);
    }
}
